package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.AddressBook;
import com.itheima.reggie.entity.User;
import com.itheima.reggie.service.AddressBookService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpSession;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/addressBook")
public class AddressBookController {
    @Autowired
    private AddressBookService addressBookService;
    //猜猜我在干什么
//<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<


    /**
     * 新增地址
     * @param addressBook
     * @param session
     * @return
     */
    @PostMapping()
    public R save( @RequestBody AddressBook addressBook , HttpSession session ){
        //aaaaaaaaaaa
        //获取用户对象
        User loginPhone = (User) session.getAttribute("loginPhone");
         //获取对象user对应的地址id
        Long id = loginPhone.getId();
        //保存该id到地址表中
        addressBook.setUserId(id);
        addressBookService.save(addressBook);
        return R.success("新增成功");
    }

    /**
     * 设置默认地址
     * @param addressBook
     * @param session
     * @return
     */
    @PutMapping("/default")
    public R setDefault(@RequestBody AddressBook addressBook,HttpSession session){
        //获取用户对象
        User loginPhone = (User) session.getAttribute("loginPhone");
        //获取对应对象user对应的地址id
        Long userId = loginPhone.getId();
        //以上前两部统一作用获取userid
        //设置查询条件,先比较用户id和数据库中的是否一致,然后默认地址设置为0
        LambdaUpdateWrapper<AddressBook> qw =new LambdaUpdateWrapper<>();
        qw.eq(AddressBook::getUserId,userId);
        qw.set(AddressBook::getIsDefault,0);
        addressBookService.update(qw);
        //设置从页面接受的用户地址为默认地址
        AddressBook addressBook1 = addressBookService.getById(addressBook.getId());

        addressBook1.setIsDefault(1);
        //保存设置
        addressBookService.updateById(addressBook1);
        return R.success("设置成功");
//        Long id = addressBook.getId();
//        AddressBook address = addressBookService.getById(id);
//        address.setIsDefault(1);
//        //保存设置
//        addressBookService.updateById(address);
//        return R.success(address);
    }

    /**
     * 根据id查询地址
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public R get (@PathVariable Long id){
        //根据id获取对象地址
        AddressBook addressBook = addressBookService.getById(id);
        if (addressBook != null) {
            return R.success(addressBook);
        } else {
            return R.error("没有找到该对象");
        }
    }

    /**
     * 查询默认地址
     * @param session
     * @return
     */
    @GetMapping("/default")
    public R getDefault(HttpSession session){
        //根据key--loginPhone来获取对象
        User loginPhone = (User) session.getAttribute("loginPhone");
        //获取对象id
        Long id = loginPhone.getId();
        //设置条件
        LambdaQueryWrapper<AddressBook> qw =new LambdaQueryWrapper<>();
        //先比较用户id是否正确
        qw.eq(AddressBook::getUserId,id);
        //继续找到默认地址
        qw.eq(AddressBook::getIsDefault,1);
        //执行查询条件,得到对象
        AddressBook addressBook = addressBookService.getOne(qw);
        if (addressBook==null){
            return R.error("没有找到对象");
        }
        return R.success(addressBook);
    }

    /**
     * 查询指定用户的全部地址
     * @param session
     * @return
     */
    @GetMapping("/list")
    public R list(HttpSession session){
        //获取指定用户
        User loginPhone = (User) session.getAttribute("loginPhone");
        //获取用户id
        Long id = loginPhone.getId();
        //获取用户地址并判断用户是否存有地址
        LambdaQueryWrapper<AddressBook> qw =new LambdaQueryWrapper<>();
        qw.eq(AddressBook::getUserId,id);
       qw.orderByDesc(AddressBook::getUpdateTime);
        List<AddressBook> list = addressBookService.list(qw);
        return R.success(list) ;
    }

    /**
     * 删除地址
     * @param ids
     * @return
     */
    @DeleteMapping
    public R deleteAddress(@PathVariable List<Long> ids){
        addressBookService.removeByIds(ids);
        return R.success("删除成功");
    }

    /**
     * 修改地址
     * @param addressBook
     * @return
     */
    @PutMapping
    public R updateAddress(@RequestBody AddressBook addressBook){
        addressBookService.updateById(addressBook);
        return R.success("修改成功");
    }

}
